// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 引入echars
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
//vuex
import store from './store/store'
// 解决移动端300ms延迟
import fastclick from 'fastclick'
fastclick.attach(document.body)
import directive from './components/global/directives'   //全局引入自定义指令
// 引入axios插件请求后台数据
import axios from 'axios';
Vue.prototype.$axios = axios;
// 全局引入Toast
import './components/Toast/toast.css';
import Toast from './components/Toast/index';
Vue.use(Toast);
//全局引入全局过滤器
import * as custom from './components/common/filter'

//这个官方名字叫导航守卫，挺形象的
router.beforeEach((to, from, next) => {

  if (to.path === '/login') {
    // 如果是去登录，那就先把token标记移除
    sessionStorage.removeItem('token');
    // 如果是去登录，那就先把当前vuex的状态标记移除
    sessionStorage.removeItem("userMsg");
    sessionStorage.removeItem('user');
    // 如果是去登录,那就初始化vuex里对比项
    store.state.indexList = [];
    // 如果是去登录,那就初始化vuex的状态
    store.commit('init')  
  } 
  let token = JSON.parse(sessionStorage.getItem('token'))
  if (!token && to.meta.requiresAuth) {   //判断是否需要登录权限
          next({ path: '/' }
    )
  } else {
    next()
  }

})
Object.keys(custom).forEach(key => {
  Vue.filter(key, custom[key])
})
Vue.prototype.msg = function (msg) {
  if (!msg) {
    return msg;
  } else {
    return msg.replace(/\\n/gm, "<br/>");
  }
}
 new Vue({
  el: '#app',
  router,
  store,//加入store对象
  components: { App },
  template: '<App/>',
});



